<template>
  <BaseForm v-model="formDataModel" @close="$emit('callBack')" />

</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import AssistDeviceRecordApi from '/@/api/webPath/AssistDeviceRecordApi.ts';
import { translate } from "/@/i18n";
import FORM_FIELD_TYPE from "/@/constants/FormFieldType.ts";
defineOptions({
  name: 'AssistDeviceRecordEdit',
})
const props = defineProps({
  modelValue: {

  }
})
const emit = defineEmits(['fetch-data', 'callBack'])

const formRef = ref<FormInstance>();
const title = ref<string>('');
const dialogFormVisible = ref<boolean>(false);

// 时间  日期


// 创建一个 Date 对象，表示当前时间
const now = new Date();

// 获取年份
const year = now.getFullYear(); // 获取完整的年份（例如：2023）

// 获取月份
const month = now.getMonth() + 1; // 获取月份（0-11，0 表示 1 月，所以需要 + 1）

// 获取日期
const day = now.getDate(); // 获取日期（1-31）

// 获取小时
const hours = now.getHours(); // 获取小时（0-23）

// 获取分钟
const minutes = now.getMinutes(); // 获取分钟（0-59）

// 获取秒数
const seconds = now.getSeconds(); // 获取秒数（0-59）

// 格式化年月日
const yearMonthDay = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;

// 格式化时分秒
const hoursMinutesSeconds = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;

const dateTime = `${yearMonthDay} ${hoursMinutesSeconds}`;

const userName = ref("");
const data = localStorage.getItem('advisory_company_userInfo')
if (data) {
  const name = JSON.parse(data)
  console.log(111111, name);
  userName.value = name.username
  console.log(222222, userName.value);
}

// 维护结果
const maintenanceResultTypeList = ref()
function getMaintenanceResultTypeList() {
  AssistDeviceRecordApi.getMaintenanceResultTypeList().then(res => {
    console.log(1234, res.data);
    maintenanceResultTypeList.value = res.data.map(item => ({ label: item.name, value: item.name }))
    console.log("canCiTypeList", maintenanceResultTypeList.value)
  })
}
// 异常处理措施
const anomalyTypeList = ref()
function getCANciTypeList() {
  AssistDeviceRecordApi.getAnomalyTypeList().then(res => {
    console.log(res.data);
    anomalyTypeList.value = res.data.map(item => ({ label: item.name, value: item.name }))
    console.log("canCiTypeList", anomalyTypeList.value)
  })
}

const formDataModel = ref({
  columnCount: 1,
  expandLimit: 6,
  autoFit: false,
  columnWidth: '300px',
  labelPosition: 'right',
  labelWidth: '100px',
  modelData: {},  //表单参数值 初始化
  callback: async (any: object) => {  //表单执行成功后回调
    //完成数据提交
    const { message } = await AssistDeviceRecordApi.save(any)
    $baseMessage(message, 'success', 'vab-hey-message-success');
    dialogFormVisible.value = false


    //事件及数据推送到上一层父类
    emit('callBack', any); //把表单数据推送到上一层
  },
  items: [  //表单item数据初始化
    {
      label: "维保日期",
      field: "serviceDate",
      clearable: true,
      type: FORM_FIELD_TYPE.DATE,
      placeholder: "请输入",
      defaultValue: yearMonthDay,

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

      config: {
        props: {   //日期精度配置
          type: "daterange",
          startPlaceholder: "开始日期",
          endPlaceholder: "结束日期",
          format: "YYYY-MM-DD",
        }
      },
    },
    {
      label: "设备类型",
      field: "deviceType",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    {
      label: "维保内容",
      field: "content",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    {
      label: "维保结果",
      field: "result",
      clearable: true,
      type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
      options: maintenanceResultTypeList,
      defaultValue: "正常",
      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    {
      label: "异常处理措施",
      field: "solution",
      clearable: true,
      type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
      options: anomalyTypeList,
      defaultValue: "/",

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    {
      label: "记录人",
      field: "operator",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,
      defaultValue: userName.value,
      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    //   {
    //     label: "所属公司",
    //     field: "companyId",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "创建人",
    //     field: "creator",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "修改人",
    //     field: "reviser",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "排序",
    //     field: "sort",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUTNUMBER,
    //     defaultValue: 0,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },

  ],
})






//页面数据加载
onMounted(async () => {
  getMaintenanceResultTypeList()
  getCANciTypeList()
  //初始化数据model
  if (props.modelValue.query) { //跳转过来时候有参数
    if (props.modelValue.query.assistDeviceRecordId) {  //id存在则为编辑，加载网络数据，如上级页面带参数手动覆盖

      const { data } = await AssistDeviceRecordApi.getDetail({
        assistDeviceRecordId: props.modelValue.query.assistDeviceRecordId   //从路由中获取参数
      })
      formDataModel.value.modelData = data;
    } else {
      //新增时候则用父类页面传参默认填充可根据业务修改
      for (const key in props.modelValue.query) {
        formDataModel.value.modelData[key] = props.modelValue.query[key]
      }
    }
  }
})


//方法暴露区域  todo
defineExpose({

})

</script>
